<?php
/*
 * 	This file is called every time we want to check if
 *	some pending operation has finished (attacks, constructions...)
 *
 *	For the moment we check:
 *		- If an attack has ocurred
 *		- If troops have arrived home after an attack
 *		- If a building construction or level improve has finished.
 *		- If a troop has been created.
 */

if (!isset($common)) require_once 'common.php';

// Needed
require_once 'include/resources.php';
include_once 'include/algoritmosCombate.php';

require 'dbconnect.php';

// ATTACKS

// Read all the registers where the time is over
$result = mysql_query("SELECT * FROM attacks WHERE hora_llegada<'".time()."'");
if( !$result)
{
	die('Error: '. mysql_error());
}

// Process each line
while($row = mysql_fetch_array($result))
{
	// TODO comprobar que funciona
	if($debug) echo 'Hay un ataque que procesar';
	batalla($row['idataque']);
}

// RETURNING

// Read all the registers where the time is over
$result = mysql_query("SELECT * FROM returning WHERE hora_llegada<'".time()."'");
if( !$result)
{
	die('Error: '. mysql_error());
}

// Process each line
while($row = mysql_fetch_array($result))
{
	if($debug) echo 'Hay una vuelta de tropas que procesar';
	
	// There are several kinds of troops - 0 to 4:
	for ($i = 0; $i<5; $i++)
	{
		// Query the previous number of that kind of troops in the city (to add +1)
		$resultTable = mysql_query("
			SELECT tropas".$i."
			FROM cities
			WHERE idcity = '".$row['id_ciudad']."' 
		");	
		if( !$resultTable )
		{
			die('Error: '. mysql_error());
		}
		$rowTable = mysql_result($resultTable, 0, 0);
			
		// Change the number of troops in the city
		if( !mysql_query("
			UPDATE cities
			SET tropas".$i." = ".($rowTable + $row["tropas".$i])."
			WHERE idcity=".$row['id_ciudad']."
		") )
		{
			die('Error: '. mysql_error());
		} 
	}	
	
	// Delete the time table
	if( !mysql_query("
		DELETE FROM returning
		WHERE idreturning = ".$row['idreturning']." 
	") )
	{
		die('Error: '. mysql_error());
	}
}

// BUILDING

// Read all the registers where the time is over
$result = mysql_query("SELECT * FROM building WHERE end_time<'".time()."'");
if( !$result)
{
	die('Error: '. mysql_error());
}

// Process each line
while($row = mysql_fetch_array($result))
{
	if($debug) echo 'Hay una construcción que procesar';
	
	// Change the box level in the city table
	if( !mysql_query("
		UPDATE cities
		SET box".$row['id_square']."lvl = ".($row['next_level'])."
		WHERE idcity=".$row['id_city']."
	") )
	{
		die('Error: '. mysql_error());
	} 	

	// Delete the time table
	if( !mysql_query("
		DELETE FROM building
		WHERE idbuilding = ".$row['idbuilding']." 
	") )
	{
		die('Error: '. mysql_error());
	}	
}

// CREATING

// Read all the registers where the time is over
$result = mysql_query("SELECT * FROM creating WHERE end_time<'".time()."'");
if( !$result)
{
	die('Error: '. mysql_error());
}

// Process each line
while($row = mysql_fetch_array($result))
{	
	if($debug) echo 'Hay una creación de tropa que procesar';
	
	// Query the previous number of that kind of troops in the city (to add +1)
	$resultTable = mysql_query("
		SELECT tropas".$row['troop_type']."
		FROM cities
		WHERE idcity = '".$row['id_city']."' 
	");	
	if( !$resultTable )
	{
		die('Error: '. mysql_error());
	}
	$rowTable = mysql_result($resultTable, 0, 0);
		
	// Change the number of troops in the city
	if( !mysql_query("
		UPDATE cities
		SET tropas".$row['troop_type']." = ".($rowTable+1)."
		WHERE idcity=".$row['id_city']."
	") )
	{
		die('Error: '. mysql_error());
	} 

	// Delete the time table
	if( !mysql_query("
		DELETE FROM creating
		WHERE idcreating = ".$row['idcreating']." 
	") )
	{
		die('Error: '. mysql_error());
	}
}

// CITY RESOURCES

// Read all the registers where the time is over
$result = mysql_query("SELECT * FROM cities WHERE last_resources_update < '".( time() - $frequency )."'");
if( !$result)
{
	die('Error: '. mysql_error());
}

// Process each line
while($row = mysql_fetch_array($result))
{
	if($debug) echo 'Hay que actualizar recursos de una ciudad<br />';
	
	// Calculate how many times the $frequency has gone
	$number = ( time() - $row['last_resources_update'] ) / $frequency;
	$number = intval($number);
	if ($number == 0) $number++;
	if($debug) echo $number.'<br />';
	
	// Calculate the new resources
	$newResources = getNewResources($row['idcity']);
	
	// Change the number of 'grano' in the city	
	if( !mysql_query("
		UPDATE cities
		SET grano = ".( $row['grano'] + $number * $newResources[0] )."
		WHERE idcity=".$row['idcity']."
	") )
	{
		die('Error: '. mysql_error());
	} 
	
	// Change the number of 'hierro' in the city
	if( !mysql_query("
		UPDATE cities
		SET hierro = ".($row['hierro'] + $number * $newResources[1] )."
		WHERE idcity=".$row['idcity']."
	") )
	{
		die('Error: '. mysql_error());
	} 
	
	// Change the number of 'madera' in the city
	if( !mysql_query("
		UPDATE cities
		SET madera = ".($row['madera'] + $number * $newResources[3] )."
		WHERE idcity=".$row['idcity']."
	") )
	{
		die('Error: '. mysql_error());
	}

	// Change the number of 'oro' in the city
	if( !mysql_query("
		UPDATE cities
		SET oro = ".($row['oro'] + $number * $newResources[2] )."
		WHERE idcity=".$row['idcity']."
	") )
	{
		die('Error: '. mysql_error());
	} 
	
	//Time to update the last_resources_update
	if( !mysql_query("
		UPDATE cities
		SET last_resources_update = ".time()."
		WHERE idcity=".$row['idcity']."
	") )
	{
		die('Error: '. mysql_error());
	} 
		
}


require 'dbdisconnect.php';
?>